/*

	This file re-constructs our data so that it mirrors the "rootfile" data set in De Loecker et al. (2016)

*/

cd ${work}2_prepare


use output\vars_ipt, clear

destring jurnr, replace
rename jurnr fid
rename vnr pcode12
rename mgd sq

*Converting everything from 1000 DK to 1 mio. DKK
gen sv=vrd/1E3
gen totsales=sales/1E3
gen s110=expenditures/1E3
gen s110_nom=expenditures_nom/1E3
gen s121=wagebill/1E3
gen fa=capital/1E3
drop vrd sales expenditures* wagebill capital

gen r=ln(totsales)
gen p_j=ln(sv/sq)
gen r_j=ln(sv)
gen q_j=ln(sq)
gen l=ln(s121)
gen k=ln(fa)
gen m=ln(s110)

rename mgd_tekst unit
gen mu=1 if missing(unit) | unit=="-"
replace mu=0 if missing(mu)
gen UNIT2=unit if mu==0
replace UNIT2="0MISSING" if mu==1

rename HS pnic2
rename HS3 pnic3
rename HS4 pnic4
rename HS6 pcode6
gen cl_pnic=pnic4
gen pcode2=pnic2
gen pcode4=pnic4
gen pcode8=pcode6
	
bysort fid year: egen sales=total(sv)
gen share_j=sv/sales
drop sales
gen rho=ln(share_j)

bysort fid year: egen sales=total(sv)
gen xmfgshare=sales/totsales
drop sales
gen m_s=m+ln(xmfgshare)

bysort fid year: gen n=_N

egen up2=group(UNIT2 pcode2)
egen up4=group(UNIT2 pcode4)
egen up6=group(UNIT2 pcode6)
egen up8=group(UNIT2 pcode8)
egen up12=group(UNIT2 pcode12) //product FE

egen fup2=group(UNIT2 pcode2 fid)
egen fup4=group(UNIT2 pcode4 fid)
egen fup6=group(UNIT2 pcode6 fid)
egen fup8=group(UNIT2 pcode8 fid)
egen fup12=group(UNIT2 pcode12 fid) //firm-product FE

egen up12y=group(UNIT2 pcode12 year) //year-product FE

gen dest_sq=dest^2
gen dest_p_sq=dest_p^2

sort fid year pcode12 UNIT2
keep fid year pcode* sq sv totsales s110* s121 fa r p_j r_j q_j l k m unit mu UNIT2 pnic* cl_pnic share_j rho xmfgshare m_s n up* fup* exp_p dest_p dest_p_sq treated_p tdcinitiative_p exp dest dest_sq treated tdcinitiative
compress
save output\lgkp_rootfile, replace